<?php

namespace app\admin\model\yuncunchu;

use think\Model;

class File extends Model
{
    // 表名
    protected $name = 'yuncunchu_file';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'file_size_text',
        'createtime_text',
        'updatetime_text'
    ];
    
    public function getStatusList()
    {
        return ['0' => __('已删除'), '1' => __('正常')];
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getFileSizeTextAttr($value, $data)
    {
        $size = isset($data['file_size']) ? $data['file_size'] : 0;
        return $this->formatBytes($size);
    }

    public function getCreatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getUpdatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['updatetime']) ? $data['updatetime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    // 格式化文件大小
    protected function formatBytes($size, $precision = 2)
    {
        $units = array('B', 'KB', 'MB', 'GB', 'TB');
        for ($i = 0; $size > 1024 && $i < count($units) - 1; $i++) {
            $size /= 1024;
        }
        return round($size, $precision) . ' ' . $units[$i];
    }

    // 关联存储配置
    public function config()
    {
        return $this->belongsTo('Config', 'config_id', 'id');
    }

    // 关联标签
    public function tags()
    {
        return $this->belongsToMany('Tag', 'yuncunchu_file_tag', 'tag_id', 'file_id');
    }

    // 获取文件类型图标
    public function getFileIcon()
    {
        $extension = strtolower($this->extension);
        $iconMap = [
            'jpg' => 'fa fa-file-image-o',
            'jpeg' => 'fa fa-file-image-o',
            'png' => 'fa fa-file-image-o',
            'gif' => 'fa fa-file-image-o',
            'bmp' => 'fa fa-file-image-o',
            'webp' => 'fa fa-file-image-o',
            'pdf' => 'fa fa-file-pdf-o',
            'doc' => 'fa fa-file-word-o',
            'docx' => 'fa fa-file-word-o',
            'xls' => 'fa fa-file-excel-o',
            'xlsx' => 'fa fa-file-excel-o',
            'ppt' => 'fa fa-file-powerpoint-o',
            'pptx' => 'fa fa-file-powerpoint-o',
            'txt' => 'fa fa-file-text-o',
            'mp4' => 'fa fa-file-video-o',
            'avi' => 'fa fa-file-video-o',
            'mov' => 'fa fa-file-video-o',
            'mp3' => 'fa fa-file-audio-o',
            'wav' => 'fa fa-file-audio-o',
            'zip' => 'fa fa-file-archive-o',
            'rar' => 'fa fa-file-archive-o',
            '7z' => 'fa fa-file-archive-o'
        ];
        
        return isset($iconMap[$extension]) ? $iconMap[$extension] : 'fa fa-file-o';
    }

    // 检查文件是否存在
    public function fileExists()
    {
        if ($this->config->type == 'local') {
            $filePath = ROOT_PATH . 'public' . $this->file_path;
            return file_exists($filePath);
        }
        // 其他云存储类型需要调用相应的API检查
        return true;
    }

    // 获取文件下载URL
    public function getDownloadUrl()
    {
        return $this->file_url;
    }
}
